package com.chanyachting.emall.dao;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.chanyachting.emall.domain.Applicationleter;
import com.chanyachting.emall.domain.Educationhistory;
import com.chanyachting.emall.domain.Jobhistory;
import com.chanyachting.emall.util.DateUtil;

public class ApplicationLeterDAO extends _BaseRootDAO {

	@Override
	protected Class getReferenceClass() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
	//修改工作历史
	public boolean updateJobHistory(Jobhistory jh){
		
		
		Transaction t = null;
		Session s = null;
		try {
			s = getSession();
			t = beginTransaction(s);
			
			Jobhistory tempJH=(Jobhistory)s.load(Jobhistory.class, jh.getId());
			
			BeanUtils.copyProperties(tempJH, jh);
			
			s.update(tempJH);
			
			commitTransaction(t);
			
			return true;
		
		}
		catch (HibernateException e) {
			if (null != t) t.rollback();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			closeSession();
		}
		
		
		return false;
	}
	
	
	
	//修改教育历史 
	public boolean updateEducationhistory(Educationhistory eh){
		
		Transaction t = null;
		Session s = null;
		try {
			s = getSession();
			t = beginTransaction(s);
			
			Educationhistory tempEH=(Educationhistory)s.load(Educationhistory.class, eh.getId());
			
			BeanUtils.copyProperties(tempEH, eh);
			
			s.update(tempEH);
			
			commitTransaction(t);
			
			return true;
		
		}
		catch (HibernateException e) {
			if (null != t) t.rollback();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			closeSession();
		}
		
		
		return false;
	}
	
	
	//添加工作历史
	public boolean addJobHistory(Applicationleter al, Jobhistory jh) {
		
		Transaction t = null;
		Session s = null;
		try {
			s = getSession();
			t = beginTransaction(s);
			
			Applicationleter  tempAL=(Applicationleter) s.load(Applicationleter.class, al.getId());
			
			jh.setApplicationleter(tempAL);
			
			tempAL.addToJobhistorySet(jh);
			
			s.save(tempAL);
			
			commitTransaction(t);
			
			return true;
		
		}
		catch (HibernateException e) {
			if (null != t) t.rollback();
		}
		finally {
			closeSession();
		}
		
		return false;
	}
	
	
	//添加教育历史 
	public boolean addEducationhistory(Applicationleter al, Educationhistory eh) {
		
		
		Transaction t = null;
		Session s = null;
		try {
			s = getSession();
			t = beginTransaction(s);
			
			Applicationleter  tempAL=(Applicationleter) s.load(Applicationleter.class, al.getId());
			
			eh.setApplicationleter(tempAL);		
			
			tempAL.addToEducationhistorySet(eh);
			
			s.save(tempAL);
			
			commitTransaction(t);
			
			return true;
		
		}
		catch (HibernateException e) {
			if (null != t) t.rollback();
		}
		finally {
			closeSession();
		}

		return false;
	}

	
	
	//刷新求职信息 
	public void refreshApplicationleter(Applicationleter al){
		
		Transaction t = null;
		Session s = null;
		try {
			s = getSession();
			t = beginTransaction(s);
			
			Applicationleter  tempAL=(Applicationleter) s.load(Applicationleter.class, al.getId());
			
			tempAL.setRefreshDate(DateUtil.getDate());
			
			s.flush();
			
			commitTransaction(t);
		
		}
		catch (HibernateException e) {
			if (null != t) t.rollback();
		}
		finally {
			closeSession();
		}
		
		
	}
	
	
	//更新信息
	public boolean updateApplicationleterInfo(Applicationleter al){
		
		Transaction t = null;
		Session s = null;
		try {
			s = getSession();
			t = beginTransaction(s);
			
			Applicationleter  tempAL=(Applicationleter) s.load(Applicationleter.class, al.getId());
			
			tempAL.setUpdateDate(DateUtil.getDate());
			
			BeanUtils.copyProperties(tempAL, al);
			
			s.flush();
			
			commitTransaction(t);
			
			return true;
		}
		catch (HibernateException e) {
			if (null != t) t.rollback();
            
			return false;
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			closeSession();
		}
		
		return false;
	
	}

	
	//更新求职信息状态
	public boolean updateApplicationleterStatus(Applicationleter al){
		
		Transaction t = null;
		Session s = null;
		try {
			s = getSession();
			t = beginTransaction(s);
			
			Applicationleter  tempAL=(Applicationleter) s.load(Applicationleter.class, al.getId());
			
			tempAL.setUpdateDate(DateUtil.getDate());
			
			tempAL.setStatus(al.getStatus());
			
			s.flush();
			
			commitTransaction(t);
			
			return true;
		}
		catch (HibernateException e) {
			if (null != t) t.rollback();
            
			return false;
		}
		finally {
			closeSession();
		}
	}
	
	public Query createQuery(String sql) {
		return this.getSession().createQuery(sql);
	}
}
